package com.nuanshui.heatedloan;

import com.nuanshui.framework.springContext.SpringContextUtil;
import com.zk.config.api.client.ConfigClient;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Properties;

/**
 * Created by zxh on 2017/6/25.
 */
public class ConfigClientFactory implements ApplicationContextAware,FactoryBean<ConfigClient> {


    /**
     * 0 远程url 1 环境变量
     */
    private Integer zkMode=0;

    public Integer getZkMode() {
        return zkMode;
    }

    public void setZkMode(Integer zkMode) {
        this.zkMode = zkMode;
    }

    private String zkHost;

    public String getZkPath() {
        return zkPath;
    }

    public void setZkPath(String zkPath) {
        this.zkPath = zkPath;
    }

    private String zkPath;

    public String getZkHost() {
        return zkHost;
    }

    public void setZkHost(String zkHost) {
        this.zkHost = zkHost;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    private Properties properties;


    @Override
    public ConfigClient getObject() throws Exception {

        String zkConfigPath;
        if(zkMode==1)
        {
            zkConfigPath=SpringContextUtil.getApplicationContext().getEnvironment().getProperty(zkHost)+zkPath;
        } else
        {
            zkConfigPath=zkHost+zkPath;
        }
        ConfigClient configClient=new ConfigClient(zkConfigPath,30000,0,
                true,properties,"admin:admin123");
        configClient.init();
        return configClient;
    }

    @Override
    public Class<?> getObjectType() {
        return ConfigClient.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

    }
}
